﻿/**
 * 1) 本插件修改自做啥JS插件；
 * 2) 本插件使用了做啥的Tag API；
 * 3）将本段代码插到网页任意处即可；<script type="text/javascript" charset="utf-8" src="http://api.li/zuosa/comments/001.js"></script>
 */

var zuosa_comments_badge = new zuosa_comments_Badge();
var count=50;
function zuosaCallback(obj){
    zuosa_comments_badge.init("zuosa_div", obj);
}



function zuosa_comments_Badge(){
	this._self = null;
	this._data = null;
	this._count = 50;
	this.init = function(id, data){
		this._self = window.document.getElementById(id);
		this._self.innerHTML =
				'<font id="zuosa_box_title" style="margin-top:0"></font>'
			+ '<div id="zuosa_update_list"></divl>'
			+ '';
		this._data = data;
		this.build();
		this.setCount(this._count);
	    this.setCount(count);
	};
	this.setCount = function(count){
		this._count = count;
		if(this._self){
			var nodes = this._self.childNodes[1].childNodes;
			for(var i = 0; i < nodes.length; i++){
				nodes[i].style.display = (i >= count && i != 50 ? "none" : "block");
			}
			nodes = null;
		}
	};
	this.build = function(){
		var sb = [];sb.push('<ol class="commentlist">');
		for(var i = 0; i < this._data.length; i++){
			var o = this._data[i];
				if (o.mms_img_pre==undefined){
				sb.push('<li class="alt"><div class="commentcount"><a href="http://zuosa.com/'+o.user.screen_name+'" target="_blank" title="'+o.user.name+'"><img src="'+o.user.profile_image_url+'" width=16 border=0 class="profile_image"></a></div><p class="comm_author"><a href="http://zuosa.com/'+o.user.screen_name+'" target="_blank" title="'+o.user.name+'">'+o.user.name+'</a></p><span class="comm_time"><a href="http://zuosa.com/statuses/' + o.id + '" target="_blank">'
					+ this.relative_time2(o.created_at) + '</a></span><p>'+o.text.replace(/\[[^\]]+\]/g,"") + '</p></li>');
					}else{
						temp = o.text.replace("http://zuosa.com/Statuses/", ""); 
						temp = temp.replace(o.id, "");
						temp = temp.replace(/\[[^\]]+\]/g,"");
						sb.push('<li class="alt"><div class="commentcount"><a href="http://zuosa.com/'+o.user.screen_name+'" target="_blank" title="'+o.user.name+'"><img src="'+o.user.profile_image_url+'" width=16 border=0 class="profile_image"></a></div><p class="comm_author"><a href="http://zuosa.com/'+o.user.screen_name+'" target="_blank" title="'+o.user.name+'">'+o.user.name+'</a></p><span class="comm_time"><a href="http://zuosa.com/statuses/' + o.id + '" target="_blank">'
					+ this.relative_time2(o.created_at) + '</a></span><p>'+ temp + '</p></li>');
					}

			o = null;
		}			sb.push('</ol>');
		this._self.childNodes[1].innerHTML = sb.join("");
		var nodes = this._self.childNodes[1].childNodes;
		var w = this._self.offsetWidth - 8;
		for(var i = 0; i < nodes.length; i++){
			nodes[i].style.width = w + "px";
		}
		nodes = null;
	};


	this.relative_time2 = function(time_value){
		var values = time_value.split(" ");
		time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		var parsed_date = Date.parse(time_value)-1000*60*60*8; //we user +0800 timezone.
		var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
		var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
		delta = delta + (relative_to.getTimezoneOffset() * 60);
		if(delta < 60){
			return '少于 1 分钟';
		}else if(delta < 120){
			return '约 1 分钟前';
		}else if(delta < (45*60)){
			return (parseInt(delta / 60)).toString() + ' 分钟前';
		}else if(delta < (90*60)){
			return '约 1 小时前';
		}else if(delta < (24*60*60)){
			return '约 ' + (parseInt(delta / 3600)).toString() + ' 小时前';
		}else if(delta < (48*60*60)){
			return '1 天前';
		}else{
			return (parseInt(delta / 86400)).toString() + ' 天前';
		}
	};
}

function zuosacomments_js(){
if (document.form1.status.value==""){document.form1.status.focus();alert("留言内容不可为空！");return false;} 
else{document.form1.status.value='[ '+location.href+' ]'+document.form1.status.value;try{document.form1.submit();}catch(er){};alert("留言正在提交, 确定后，稍后会显示，多谢您的支持！");return true;}

}

document.write('<div id="zuosa_div"><div id="zuosa_update_list"></div></div><script text="text/javascript" src="http://api.zuosa.com/tag/search.json?&callback=zuosaCallback&tag='+encodeURIComponent(location.href)+'"></script>'); 